package com.ding.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ding.commonutils.Result;
import com.ding.eduservice.entity.EduChapter;
import com.ding.eduservice.entity.EduVideo;
import com.ding.eduservice.entity.chapter.ChapterVo;
import com.ding.eduservice.entity.chapter.VideoVo;
import com.ding.eduservice.mapper.EduChapterMapper;
import com.ding.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ding.eduservice.service.EduVideoService;
import com.ding.servicebase.exceptionmanage.Dingexception;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-16
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    @Override
    public List<ChapterVo> getChapterByCourseId(String courseId) {
        //根据课程id查询出课程对应的章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //根据课程id查询出课程对应的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> videolist = eduVideoService.list(wrapperVideo);

        //创建list集合，用于最终封装数据
        List<ChapterVo> finalList = new ArrayList<>();

        //遍历查询章节list集合进行封装
        for (int i = 0; i < eduChapterList.size(); i++) {
            EduChapter eduChapter = eduChapterList.get(i);
            //eduChapter对象中的值复制到ChapterVo里面去
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //chapterVo放到list集合中去
            finalList.add(chapterVo);

            //创建集合，用于封装章节中的小节
            List<VideoVo> finalVideo = new ArrayList<>();

            for (int i1 = 0; i1 < videolist.size(); i1++) {
                EduVideo eduVideo = videolist.get(i1);
               if(chapterVo.getId().equals(eduVideo.getChapterId())){
                   VideoVo videoVo = new VideoVo();
                   BeanUtils.copyProperties(eduVideo,videoVo);
                   finalVideo.add(videoVo);
               }
            }
            //将小节的list集合封装到章节的list集合中去
            chapterVo.setChildren(finalVideo);
        }

        //遍历查询小节list集合进行封装

        return finalList;
    }

    @Override
    public int addChapterdata(EduChapter eduChapter) {
        int insert = baseMapper.insert(eduChapter);
        return insert;
    }

    @Override
    public EduChapter getInfo(String chaptedId) {
        EduChapter eduChapter = baseMapper.selectById(chaptedId);
        return eduChapter;
    }

    @Override
    public Result updateChapterdata(EduChapter eduChapter) {
        System.out.println(eduChapter);
         eduChapterService.updateById(eduChapter);
        return Result.ok();
    }

    //删除章节的方法
    @Override
    public int delChapterdata(String chaptedId) {
        //根据chaptedId查询小节，如果有数据就进行删除，没有就不删除
        QueryWrapper<EduVideo> EduVideoWrapper = new QueryWrapper<>();
        EduVideoWrapper.eq("chapter_id",chaptedId);
        //看一下里面有没有值
        int eduVideor = eduVideoService.count(EduVideoWrapper);
        int a= 0;

        if(eduVideor == 0){
            int i = baseMapper.deleteById(chaptedId);
            a = i;
        }else{
            throw new Dingexception(20001,"小节有数据不能删除");
        }
        return a;
    }

    @Override
    public void delchapterByCourseId(String courseId) {
        baseMapper.deleteById(courseId);
    }
}
